package cn.wolfcode.wolf2w.service.impl;

import cn.wolfcode.wolf2w.domain.Banner;
import cn.wolfcode.wolf2w.domain.Strategy;
import cn.wolfcode.wolf2w.domain.Travel;
import cn.wolfcode.wolf2w.query.QueryObject;
import cn.wolfcode.wolf2w.repository.BannerRepository;
import cn.wolfcode.wolf2w.service.IBannerService;
import cn.wolfcode.wolf2w.service.IStrategyService;
import cn.wolfcode.wolf2w.service.ITravelService;
import cn.wolfcode.wolf2w.util.DBHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class BannerServiceImpl implements IBannerService{


    @Autowired
    private BannerRepository bannerRepository;
    @Autowired
    private IStrategyService strategyService;
    @Autowired
    private ITravelService travelService;

    @Autowired
    private MongoTemplate template;

    @Override
    public void save(Banner banner) {
        banner.setId(null);
        bannerRepository.save(banner);
    }

    @Override
    public void delete(String id) {
        bannerRepository.deleteById(id);
    }

    @Override
    public void update(Banner banner) {
        bannerRepository.save(banner);
    }

    @Override
    public Banner get(String id) {
        Banner banner = bannerRepository.findById(id).orElse(null);
        return banner;
    }

    @Override
    public List<Banner> list() {

        return bannerRepository.findAll();
    }

    @Override
    public Page<Banner> queryPageList(QueryObject qo) {
        Query query = new Query();
        Pageable pageable= PageRequest.of(qo.getCurrentPage()-1, qo.getPageSize(), Sort.Direction.ASC, "_id");
        return DBHelper.query(template,Banner.class,query,pageable);
    }

    @Override
    public Map getAllType() {
        Map map = new HashMap<>();
        //获取所有攻略
        List<Strategy> sts = strategyService.list();
        //获取所有游记
        List<Travel> ts = travelService.list();
        map.put("sts",sts);
        map.put("ts",ts);
        return map;
    }

    @Override
    public List<Banner> queryList(int num) {
        Pageable pageable = PageRequest.of(0,5, Sort.Direction.ASC,"_id");
        List<Banner> ts = bannerRepository.findByType(num, pageable);
        return ts;
    }

}
